package com.apexsoft.monitor.front.console.config.service;

import com.alibaba.fastjson.JSONObject;
import com.apexsoft.front.common.support.Result;
import com.apexsoft.monitor.front.console.config.dao.ConfigDao;
import com.apexsoft.monitor.front.console.config.om.Config;
import com.apexsoft.monitor.front.console.pageModel.ConfigModel;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service("configService")
public class ConfigService {

    private static final Logger log = LogManager.getRootLogger();

    @Autowired
    @Qualifier("configDao")
    private ConfigDao configDao = null;

    public JSONObject listByPage(HttpServletRequest request,Pagination page, ConfigModel pageModel) throws Exception {

        EntityWrapper<Config> entityWrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(pageModel.getConfigCode())) {
            entityWrapper.and("config_code={0}", pageModel.getConfigCode());
        }
        if (StringUtils.isNotBlank(pageModel.getConfigName())) {
            entityWrapper.like("config_name", pageModel.getConfigName());
        }
        if(StringUtils.isNotBlank(pageModel.getConfigValue())){
            entityWrapper.like("config_value", pageModel.getConfigValue());
        }
        if (pageModel.getConfigStatus()!=null){
            entityWrapper.and("config_status={0}",pageModel.getConfigStatus());
        }
        List<Config> list = configDao.selectPage(page, entityWrapper);

        JSONObject result = Result.commonListResult(list);
        return result;

    }

    public JSONObject update(HttpServletRequest request) {

        JSONObject result = new JSONObject();
        String id = request.getParameter("id");
        String configName = request.getParameter("configName");
        String configCode = request.getParameter("configCode");
        String configValue = request.getParameter("configValue");
        String configDesc = request.getParameter("configDesc");
        String configStatus = request.getParameter("configStatus");
        if (StringUtils.isNotBlank(configValue)) {
            Config config = new Config();
            config.setId(Integer.parseInt(id));
            config.setConfigCode(configCode);
            config.setConfigName(configName);
            config.setConfigValue(configValue);
            config.setConfigDesc(configDesc);
            config.setConfigStatus(StringUtils.isNumeric(configStatus)?Integer.parseInt(configStatus):0);
            try {
                int uptRst = configDao.updateSelectiveById(config);
                if (uptRst> Result.EC_1000) {
                    result.put(Result.CODE, Result.SC_0001);
                    result.put(Result.NOTE, "修改阀值成功");
                }
            } catch (Exception e) {
                log.error("修改阀值失败："+e.getMessage(), e);
                result.put(Result.CODE, Result.EC_1000);
                result.put(Result.NOTE, "修改阀值失败");
            }

        }else {
            result.put(Result.CODE, Result.EC_1000);
            result.put(Result.NOTE, "参数不正确");
        }
        return result;
    }

    public JSONObject add(HttpServletRequest request) {

        JSONObject result = new JSONObject();
        String configName = request.getParameter("configName");
        String configCode = request.getParameter("configCode");
        String configValue = request.getParameter("configValue");
        String configDesc = request.getParameter("configDesc");
        String configStatus = request.getParameter("configStatus");

        if (StringUtils.isNotBlank(configName)
                && StringUtils.isNotBlank(configCode)
                && StringUtils.isNotBlank(configValue)
                && StringUtils.isNotBlank(configStatus)
                ) {

            Config config = new Config();
            config.setConfigCode(configCode);
            config.setConfigName(configName);
            config.setConfigDesc(configDesc);
            config.setConfigValue(configValue);
            config.setConfigStatus(StringUtils.isNumeric(configStatus)?Integer.parseInt(configStatus):0);
            try {
                Config tempConfig = configDao.selectByConfigCode(configCode);
                if (tempConfig!=null){
                    result.put(Result.CODE, Result.EC_1000);
                    result.put(Result.NOTE, "阀值代码["+configCode+"]已存在");
                    return result;
                }
                long insertRst = configDao.insert(config);
                if (insertRst> Result.EC_1000) {
                    result.put(Result.CODE, Result.SC_0001);
                    result.put(Result.NOTE, "新增阀值成功");
                }
            } catch (Exception e) {
                log.error("新增阀值失败："+e.getMessage(), e);
                result.put(Result.CODE, Result.EC_1000);
                result.put(Result.NOTE, "新增阀值失败");
            }

        }else {
            result.put(Result.CODE, Result.EC_1000);
            result.put(Result.NOTE, "参数不正确");
        }
        return result;
    }


    public JSONObject delete(String id) {

        JSONObject result = new JSONObject();
        if (StringUtils.isNotBlank(id)) {

            try {
                int delRst = configDao.deleteById(id);
                if (delRst> Result.EC_1000) {
                    result.put(Result.CODE, Result.SC_0001);
                    result.put(Result.NOTE, "删除阀值成功");
                }
            } catch (Exception e) {
                log.error("删除阀值失败："+e.getMessage(), e);
                result.put(Result.CODE, Result.EC_1000);
                result.put(Result.NOTE, "删除阀值失败");
            }


        }else {
            result.put(Result.CODE, Result.EC_1000);
            result.put(Result.NOTE, "参数不正确");
        }
        return result;
    }

    public Config getById(int id) {
        return configDao.selectById(id);
    }
    public Config getByConfigCode(String code) {
        return configDao.selectByConfigCode(code);
    }

}


